import Request from "luch-request"
import { baseUrl } from "@/common/config/config.js"
import { useUserStore } from "@/store/user"

const http = new Request({
  baseURL: baseUrl
})
// 请求拦截器
http.interceptors.request.use((config) => {
  const store = useUserStore()
  if (store.token) {
    config.header.token = store.token
  }
  return config
}, config => {
  return Promise.reject(config)
})
// 返回拦截器
http.interceptors.response.use((response) => {
  setTimeout(() => {
    uni.hideLoading()
  }, 1500)
  const { config, data, statusCode } = response
  console.log('== http response ==', config.method, config.url, data)
  const customConfig = config.custom
  // 是否需要拦截
  const needIntercept = customConfig.needIntercept ?? true
  if (!needIntercept) return response

  const { code, msg } = data
  // 是否要显示toast
  const showToast = customConfig.showToast ?? true

  if (statusCode !== 200) {
    if (showToast) {
      uni.showToast({
        icon: (msg && msg.length > 6) ? 'none' : 'error',
        title: msg || '请求出错'
      })
    }
    return Promise.reject(msg)
  }

  if (code != 1) {
    if (showToast) {
      uni.showToast({
        icon: msg?.length >= 6 ? 'none' : 'error',
        title: msg || '请求出错'
      })
    }
    if (code == 401) {
      checkAndToLogin()
    } 
    return Promise.reject(msg)
  }
  return data
}, (response) => {
  console.log('=== request error ===', response);
  setTimeout(() => {
    uni.hideLoading()
  }, 1500)

  const { code, msg } = response.data
  const showToast = response.config.custom.showToast ?? true
  if (showToast) {
    uni.showToast({
      icon: msg?.length >= 6 ? 'none': 'error',
      title: msg || "请求出错"
    })
  }

  if (code == 401) {
    checkAndToLogin()
  }
  return Promise.reject(response)
})

function checkAndToLogin() {
  useUserStore().clear()
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1].route
  const loginPage = 'package_user/pages/common/login'
  if (currentPage != loginPage) {
    uni.navigateTo({
      url: '/' + loginPage
    })
  }
}

const checkAndShowLoading = (showLoading, loadingText) => {
  if (showLoading) {
    uni.showLoading({
      title: loadingText
    })
  }
}

export const get = (url, params, options = {}, customConfig) => {
  checkAndShowLoading(options.showLoading, options.loadingText || '正在加载')
  return http.get(url, { params, custom: customConfig })
}

export const post = (url, data, params, options = {}, customConfig) => {
  checkAndShowLoading(options.showLoading ?? true, options.loadingText || '正在提交')
  return http.post(url, data, { params, custom: customConfig })
}

export const put = (url, data, params, options = {}, customConfig) => {
  checkAndShowLoading(options.showLoading ?? true, options.loadingText || '正在提交')
  return http.put(url, data, { params, custom: customConfig })
}

export const del = (url, data, params, options = {}, customConfig) => {
  checkAndShowLoading(options.showLoading ?? true, options.loadingText || '正在提交')
  return http.delete(url, data, { params, custom: customConfig })
}

export const upload = (url, file, options = {}) => {
  checkAndShowLoading(options.showLoading ?? true, options.loadingText || '正在上传')
  return http.upload(url, { name: 'file', filePath: file })
}